#include "OKIN_drive_adc.h"
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "nrf.h"
#include "nrf_drv_saadc.h"
#include "nrf_drv_ppi.h"
#include "nrf_drv_timer.h"
#include "boards.h"
#include "app_error.h"
#include "nrf_delay.h"
#include "app_util_platform.h"
#include "nrf_pwr_mgmt.h"

#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"
#include "board.h"
#define SAMPLES_IN_BUFFER 5
volatile uint8_t state = 1;






void saadc_callback(nrf_drv_saadc_evt_t const * p_event)
{
    if (p_event->type == NRF_DRV_SAADC_EVT_DONE)
    {
        ;
    }
}





void OKIN_drive_saadc_init(void)
{
	nrf_saadc_channel_config_t channel_config1 =NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN3);
	nrf_drv_saadc_init(NULL, saadc_callback);
	nrf_drv_saadc_channel_init(OKIN_DRIVE_ADCCH_VOLTAGE, &channel_config1);

	nrf_saadc_channel_config_t channel_config2 =NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN2);
	nrf_drv_saadc_init(NULL, saadc_callback);
	nrf_drv_saadc_channel_init(OKIN_DRIVE_ADCCH_M2, &channel_config2);

	nrf_saadc_channel_config_t channel_config3 =NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN1);
	nrf_drv_saadc_init(NULL, saadc_callback);
	nrf_drv_saadc_channel_init(OKIN_DRIVE_ADCCH_M1, &channel_config3);
	
	nrf_saadc_channel_config_t channel_config4 =NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN6);
	nrf_drv_saadc_init(NULL, saadc_callback);
	nrf_drv_saadc_channel_init(OKIN_DRIVE_ADCCH_VOLTAGE_2, &channel_config4);
}



void OKIN_drive_saadc_get(uint8_t ch, nrf_saadc_value_t *saadc_val)
{
	nrf_drv_saadc_sample_convert(ch,saadc_val);	
}

void OKIN_drive_test(void)
{
	static  int i = 0;
	int16_t temp = 0;
	i++;
	OKIN_drive_saadc_get(OKIN_DRIVE_ADCCH_VOLTAGE,&temp);
	NRF_LOG_INFO("%d adc = %d\r\n",i,temp);
}

